{% extends "../layouts/admin.html" %} {% block content %}

<header class="header navbar bg-white shadow">

<div class="btn-group tool-button">
	<!--<a class="btn btn-primary navbar-btn pull-left" onclick="updateJsonOrder()"><i class="ti-reload"></i> Json同步订单</a>
	<a class="btn btn-primary navbar-btn pull-left" onclick="updateExcelOrder()"><i class="ti-reload"></i> Excel同步订单</a>-->
	<div class="form-group navbar-btn  pull-left mr5">
		<select name="period" id="period" class="form-control">
			<option value="" selected disabled>选择同步时间</option>
			<option value="1">同步今日订单</option>
			<option value="2">同步两天订单</option>
			<option value="7">同步一周订单</option>
			<option value="15">同步15天订单</option>
			<option value="30">同步30天订单</option>
			<option value="60">同步60天订单</option>
		</select>
	</div>
	<a class="btn btn-primary navbar-btn pull-left" onclick="updateOrder()"><i class="ti-reload"></i> 同步订单</a>
	<!--<div class="form-group navbar-btn  pull-left">
		<select name="status" id="status" class="form-control">
			<option value="" selected disabled>订单状态</option>
			<option value="3">订单结算</option>
			<option value="12">订单付款</option>
			<option value="13">订单失效</option>
		</select>
	</div>-->
	</div>
	<div class="pull-right offscreen-right">
		<button class="btn btn-primary navbar-btn" onclick="sublime.toggleFilter('.cd-panel')"><i
          class="fa fa-sliders"></i> 筛选
      </button>
	</div>
</header>
<div class=panel-body style="padding-top: 50px;">
	<div class="table-responsive no-border">
		<input id="unitid" type="hidden">
		<table class="table table-bordered table-striped mg-t datatable">
			<thead>
				<tr>
					<th>订单编号</th>
					<th>商品名称</th>
					<!--<th>用户账户</th>-->
					<th>用户昵称</th>
					<th>付款金额</th>
					<th>佣金比例</th>
					<th>总佣金</th>
					<th>绑单人分红比例</th>
					<th>绑单人佣金</th>
					<th>推荐人分红比例</th>
					<th>推荐人佣金</th>
					<th>订单状态</th>
					<th>创建时间</th>
					<th>完结时间</th>

					<th>操作</th>
		</table>
	</div>
</div>
<div class="cd-panel from-right">
	<header class="cd-panel-header">
		<h4>高级筛选</h4>
	</header>
	<div class="cd-panel-container">
		<div class="cd-panel-content shadow">
			<div class="form-group">
				<label for="order_sn">订单编号</label>
				<input type="text" id="order_sn" name="order_sn" onclick="this.value=''" class="form-control" placeholder="订单编号">
			</div>
			<div class="form-group">
				<label for="username">订单状态</label>
				<select name="status" id="status" class="form-control">
			<option value="" selected>订单状态</option>
			<option value="3">订单结算</option>
			<option value="12">订单付款</option>
			<option value="13">订单失效</option>
		</select>
			</div>
			<div class="form-group">
				<label for="username">用户名</label>
				<input type="text" id="username" name="username" onclick="this.value=''" class="form-control" placeholder="用户名/登录名" autofocus>
			</div>
			<div class="form-group">
				<label for="nickname">姓名</label>
				<input type="text" id="nickname" name="nickname" onclick="this.value=''" class="form-control" placeholder="姓名/昵称">
			</div>
			<button id="searchBtn" type="button" class="btn btn-default">查询</button>
		</div>
	</div>
</div>
<div id="dialogBind" class="modal fade bs-modal-sm" tabindex="-2" role="dialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title">修改绑单人</h4>
			</div>
			<div class="modal-body">
				<div class="row">
					<div class="col-xs-12 form-horizontal parsley-form">
						<div class="form-group">
							<label for="username" class="col-sm-2 control-label">用户账号</label>

							<div class="col-sm-8">
								<input type="text" id="username" class="form-control" name="username" data-parsley-required="true" placeholder="绑定用户账号">
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
				<button id="ok" type="button" class="btn btn-primary" data-loading-text="正在修改...">确 定</button>
			</div>
		</div>
	</div>
</div>

<div id="dialogClear" class="modal fade bs-modal-sm" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title">清除绑单</h4>
			</div>
			<div class="modal-body">
				<div class="row">
					<div class="col-xs-12">
						确定清除该订单绑定的账户吗？
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
				<button id="ok" type="button" class="btn btn-primary" data-loading-text="正在清除...">确 定</button>
			</div>
		</div>
	</div>
</div>
<div id="balanceOrder" class="modal fade bs-modal-sm" tabindex="-3" role="dialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title">结算订单</h4>
			</div>
			<div class="modal-body">
				<div class="row">
					<div class="col-xs-12">
						确定对该订单进行结算吗？
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
				<button id="ok" type="button" class="btn btn-primary" data-loading-text="正在结算...">确 定</button>
			</div>
		</div>
	</div>
</div>

<div id="updateOrder" class="modal fade bs-modal-sm" tabindex="-4" role="dialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title">同步订单</h4>
			</div>
			<div class="modal-body">
				<div class="row">
					<div class="col-xs-12">
						确定进行订单同步吗？ <br />订单同步随所选时间快慢不同，请耐心等候
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
				<button id="ok" type="button" class="btn btn-primary" data-loading-text="正在同步...">确 定</button>
			</div>
		</div>
	</div>
</div>

<script language="JavaScript">
	var datatable;

	function initDatatable() {
		datatable = $('.datatable').DataTable({
			"dom": '<"toolbar">frtip',
			"searching": false,
			"processing": false,
			"serverSide": true,
			"select": true,
			"autoWidth": true,
			 "scrollX": true,

			"ordering": true,
			"language": {
				"url": "/assets/plugins/datatables/cn.json"
			},
			"preDrawCallback": function() {
				sublime.showLoadingbar($(".main-content"));
			},
			"drawCallback": function() {
				sublime.closeLoadingbar($(".main-content"));
			},
			"ajax": {
				"url": "/taoke/order/index",
				"type": "post",
				"data": function(d) {
					d.username = $('#username').val();
					d.nickname = $('#nickname').val();
					d.order_sn = $('#order_sn').val();
					d.status = $('#status').val();
				}
			},
			"order": [
				[11, "desc"]
			],
			"columns": [{
					"data": "order_sn",
					"bSortable": true
				}, {
					"data": "goods_name",
					"bSortable": true
				},
//				{
//					"data": "username",
//					"bSortable": true
//				},
				{
					"data": "nickname",
					"bSortable": true
				},
				{
					"data": "order_amount",
					"bSortable": true
				},
				{
					"data": "income_ratio",
					"bSortable": true
				},
				{
					"data": "effect_prediction",
					"bSortable": true
				},
				{
					"data": "buyer_rate",
					"bSortable": true
				},
				{
					"data": "buyer_commission",
					"bSortable": true
				},
				{
					"data": "recommend_rate",
					"bSortable": true
				},
				{
					"data": "recommend_commission",
					"bSortable": true
				},
				{
					"data": "order_status",
					"bSortable": true
				},
				{
					"data": "create_time",
					"bSortable": true
				},
				{
					"data": "balance_time",
					"bSortable": true
				}
			],
			"columnDefs": [{
				"render": function(data, type, row) {

					return '<div class="btn-group"><button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">' +
						' <i class="ti-settings"></i> <span class="ti-angle-down"></span></button><ul class="dropdown-menu" role="menu">' +
						'<li><a onclick="editBind(' + row.id + ')">修改绑单人</a></li>' +
						'<li><a onclick="clearBind(' + row.id + ')">清除绑单</a></li>' +
						'<li><a onclick="balanceOrder(' + row.id + ')">结算订单</a></li>' +

						'</ul></div>';
				},
				"targets": 13
			}]
		});
		datatable.on('click', 'tr', function() {
			$(this).toggleClass('selected');
		});
		$("#searchBtn").on('click', function() {
			datatable.ajax.reload();
		});
		
//		$("#status").on('change', function() {
//			datatable.ajax.reload();
//		});
	}

	function editBind(id) {
		var dialog = $("#dialogBind");
		dialog.modal("show");
		dialog.find("#ok").unbind("click");

		dialog.find("#ok").bind("click", function(event) {
			var btn = $(this);
			btn.button("loading");
			var username = dialog.find("#username").val();
			$.post("/taoke/order/edit", {
				id: id,
				username: username
			}, function(data) {
				if(data.errno == 0) {
					datatable.ajax.reload(null, false);
				} else {
					Toast.error(data.errmsg);
				}
				//重置按钮状态，关闭提示框
				btn.button("reset");
				dialog.modal("hide");
			}, "json");
		});
	}

	function clearBind(id) {
		console.log("11111111111")
		var dialog = $("#dialogClear");
		console.log("11111111111")
		dialog.modal("show");
		dialog.find("#ok").unbind("click");
console.log("11111111111")
		dialog.find("#ok").bind("click", function(event) {
			var btn = $(this);
			btn.button("loading");
console.log("2222222222222")
			$.post("/taoke/order/clear", {
				id: id
			}, function(data) {
				if(data.errno == 0) {
					datatable.ajax.reload(null, false);
				} else {
					Toast.error(data.errmsg);
				}
				//重置按钮状态，关闭提示框
				btn.button("reset");
				dialog.modal("hide");
			}, "json");
		});
	}
	
	function balanceOrder(id) {
		
		var dialog = $("#balanceOrder");
		dialog.modal("show");
		dialog.find("#ok").unbind("click");
		dialog.find("#ok").bind("click", function(event) {
			var btn = $(this);
			btn.button("loading");
			$.post("/taoke/order/balance", {
				id: id
			}, function(data) {
				if(data.errno == 0) {
					datatable.ajax.reload(null, false);
				} else {
					Toast.error(data.errmsg);
				}
				//重置按钮状态，关闭提示框
				btn.button("reset");
				dialog.modal("hide");
			}, "json");
		});
	}
	
	function updateExcelOrder() {

			var dialog = $("#updateOrder");
		dialog.modal("show");
		dialog.find("#ok").unbind("click");
		dialog.find("#ok").bind("click", function(event) {
			var btn = $(this);
			btn.button("loading");
			$.post("/taoke/order/updateExcel", {
			}, function(data) {
				if(data.errno == 0) {
					datatable.ajax.reload(null, false);
				} else {
					Toast.error(data.errmsg);
				}
				//重置按钮状态，关闭提示框
				btn.button("reset");
				dialog.modal("hide");
			}, "json");
		});
		}
	function updateJsonOrder() {

			var dialog = $("#updateOrder");
		dialog.modal("show");
		dialog.find("#ok").unbind("click");
		dialog.find("#ok").bind("click", function(event) {
			var btn = $(this);
			btn.button("loading");
			$.post("/taoke/order/updateJson", {
			}, function(data) {
				if(data.errno == 0) {
					datatable.ajax.reload(null, false);
				} else {
					Toast.error(data.errmsg);
				}
				//重置按钮状态，关闭提示框
				btn.button("reset");
				dialog.modal("hide");
			}, "json");
		});
		}
	function updateOrder() {
var period = $("#period").val();
		if(!period){
			return Toast.error("请先选择订单同步时间周期");
		}
			var dialog = $("#updateOrder");
		dialog.modal("show");
		dialog.find("#ok").unbind("click");
		
		dialog.find("#ok").bind("click", function(event) {
			var btn = $(this);
			btn.button("loading");
			
			$.post("/taoke/order/updateApi", {
				period:period
			}, function(data) {
				if(data.errno == 0) {
					Toast.success(data.errmsg);
					//datatable.ajax.reload(null, false);
				} else {
					Toast.error(data.errmsg);
				}
				//重置按钮状态，关闭提示框
				btn.button("reset");
				dialog.modal("hide");
			}, "json");
			dialog.find("#ok").button("reset");
			dialog.modal("hide");
			Toast.success("订单同步请求已提交");
		});
		}
	

	$(function() {
		initDatatable();
	});
</script>
{% endblock %}